﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.XtraCharts;

namespace Thi_nghiem_nen_ket_co_thong_tin
{
    public partial class Cv_P_and_Kv_e : System.Web.UI.UserControl
    {
        public int NumOfData {
            get { return string.IsNullOrEmpty(HnoOfData.Value) ? 0 : Convert.ToInt32(HnoOfData.Value); }
            set { HnoOfData.Value = value.ToString(); }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            //if (!IsPostBack)
            {
                LoadData();
            }

        }

        public void LoadData()
        {
            var objTn = (ObjThiNghiem)Session[ObjThiNghiem.SESSION_NAME];
            if (objTn == null || NumOfData <= 0)
            {
                dataGridView_p_cv.DataSource = null;
                dataGridView_kv_e.DataBind();
                chartControl_p_cv.DataSource = null;
                chartControl_p_cv.DataBind();
                chartControl_kv_e.DataSource = null;
                chartControl_kv_e.DataBind();
                return;
            }
            double[] P = objTn.P;
            double[] Cv = objTn.Cv;
            double[] Kv = objTn.Kv;
            double[] hsr = objTn.hsr;
           // Create an empty Bar series and add it to the chart.
            Series series_p_cv = new Series("series_p_cv", ViewType.ScatterLine);
            // Specify data members to bind the series.
            series_p_cv.ArgumentScaleType = ScaleType.Numerical;
            series_p_cv.ArgumentDataMember = "Argument";
            series_p_cv.ValueScaleType = ScaleType.Numerical;
            series_p_cv.ValueDataMembers.AddRange(new string[] { "Value" });
            chartControl_p_cv.Series.Add(series_p_cv);
            chartControl_p_cv.Legend.Visible = false;
            // Dock the chart into its parent and add it to the current form.
            ((XYDiagram)chartControl_p_cv.Diagram).AxisX.Title.Visible = true;
            ((XYDiagram)chartControl_p_cv.Diagram).AxisX.Title.Alignment = StringAlignment.Center;
            ((XYDiagram)chartControl_p_cv.Diagram).AxisX.Title.Text = "Áp lực nén";
            ((XYDiagram)chartControl_p_cv.Diagram).AxisX.Title.TextColor = Color.Red;
            ((XYDiagram)chartControl_p_cv.Diagram).AxisX.Title.Antialiasing = true;
            ((XYDiagram)chartControl_p_cv.Diagram).AxisX.Title.Font = new Font("Tahoma", 12, FontStyle.Regular);
            // Customize the appearance of the Y-axis title.
            ((XYDiagram)chartControl_p_cv.Diagram).AxisY.Title.Visible = true;
            ((XYDiagram)chartControl_p_cv.Diagram).AxisY.Title.Alignment = StringAlignment.Center;
            ((XYDiagram)chartControl_p_cv.Diagram).AxisY.Title.Text = "Hệ số cố kết";
            ((XYDiagram)chartControl_p_cv.Diagram).AxisY.Title.TextColor = Color.Blue;
            ((XYDiagram)chartControl_p_cv.Diagram).AxisY.Title.Antialiasing = true;
            ((XYDiagram)chartControl_p_cv.Diagram).AxisY.Title.Font = new Font("Tahoma", 12, FontStyle.Regular);

            ((XYDiagram)chartControl_p_cv.Diagram).AxisY.VisualRange.Auto = true;
            

            // Create an empty table.
            DataTable table_p_cv = new DataTable("Table1");

            // Add two columns to the table.
            table_p_cv.Columns.Add("Argument", typeof(Double));
            table_p_cv.Columns.Add("Value", typeof(Double));

            // Add data rows to the table.
            DataRow row_p_cv = null;

            for (int j = 1; j <= NumOfData; j++)
            {
                row_p_cv = table_p_cv.NewRow();
                var ar = Math.Log10(P[j]);
                if (!double.IsInfinity(ar) && !double.IsNaN(ar) && !double.IsInfinity(Cv[j]) && !double.IsNaN(Cv[j]))
                {
                    row_p_cv["Argument"] = ar;
                    row_p_cv["Value"] = Cv[j];
                    table_p_cv.Rows.Add(row_p_cv);
                }
            }
            series_p_cv.DataSource = table_p_cv;

         
            chartControl_p_cv.DataBind();

            var dtCv = new DataTable();
            dtCv.Columns.Add("Name");
            for (int k = 1; k <= 7; k++)
            {
                dtCv.Columns.Add(k.ToString());
            }
            dtCv.Rows.Add(new object[] {"P", P[1], P[2], P[3], P[4], P[5], P[6], P[7]});
            dtCv.Rows.Add(new object[] { "Cv", Cv[1], Cv[2], Cv[3], Cv[4], Cv[5], Cv[6], Cv[7] });
            dataGridView_p_cv.DataSource = dtCv;
            dataGridView_p_cv.DataBind();
            //<<=============================================================================>>//
            

            // Create an empty Bar series and add it to the chart.
            Series series_kv_e = new Series("series_kv_e", ViewType.ScatterLine);
            series_kv_e.ArgumentScaleType = ScaleType.Numerical;
            series_kv_e.ArgumentDataMember = "Argument";
            series_kv_e.ValueScaleType = ScaleType.Numerical;
            series_kv_e.ValueDataMembers.AddRange(new string[] { "Value" });
            chartControl_kv_e.Series.Add(series_kv_e);
            // Specify data members to bind the series.

            chartControl_kv_e.Legend.Visible = false;
            // Dock the chart into its parent and add it to the current form.
            ((XYDiagram)chartControl_kv_e.Diagram).AxisX.Title.Visible = true;
            ((XYDiagram)chartControl_kv_e.Diagram).AxisX.Title.Alignment = StringAlignment.Center;
            ((XYDiagram)chartControl_kv_e.Diagram).AxisX.Title.Text = "Hệ số thấm";
            ((XYDiagram)chartControl_kv_e.Diagram).AxisX.Title.TextColor = Color.Red;
            ((XYDiagram)chartControl_kv_e.Diagram).AxisX.Title.Antialiasing = true;
            ((XYDiagram)chartControl_kv_e.Diagram).AxisX.Title.Font = new Font("Tahoma", 12, FontStyle.Regular);
            // Customize the appearance of the Y-axis title.
            ((XYDiagram)chartControl_kv_e.Diagram).AxisY.Title.Visible = true;
            ((XYDiagram)chartControl_kv_e.Diagram).AxisY.Title.Alignment = StringAlignment.Center;
            ((XYDiagram)chartControl_kv_e.Diagram).AxisY.Title.Text = "Hệ số rỗng";
            ((XYDiagram)chartControl_kv_e.Diagram).AxisY.Title.TextColor = Color.Blue;
            ((XYDiagram)chartControl_kv_e.Diagram).AxisY.Title.Antialiasing = true;
            ((XYDiagram)chartControl_kv_e.Diagram).AxisY.Title.Font = new Font("Tahoma", 12, FontStyle.Regular);

            ((XYDiagram)chartControl_kv_e.Diagram).AxisY.VisualRange.Auto = true;
           

            // Create an empty table.
            DataTable table_kv_e = new DataTable("Table1");

            // Add two columns to the table.
            table_kv_e.Columns.Add("Argument", typeof(Double));
            table_kv_e.Columns.Add("Value", typeof(Double));

            // Add data rows to the table.
            DataRow row_kv_e = null;

            for (int j = 1; j <= NumOfData; j++)
            {
                row_kv_e = table_kv_e.NewRow();
                if (!double.IsInfinity(Kv[j]) && !double.IsNaN(Kv[j]) && !double.IsInfinity(hsr[j]) && !double.IsNaN(hsr[j]))
                {
                    row_kv_e["Argument"] = Kv[j];
                    row_kv_e["Value"] = hsr[j];
                    table_kv_e.Rows.Add(row_kv_e);
                }
            }
            series_kv_e.DataSource = table_kv_e;

            chartControl_p_cv.DataBind();
            chartControl_kv_e.DataBind();

            var dtKv = new DataTable();
            dtKv.Columns.Add("Name");
            for (int k = 1; k <= 7; k++)
            {
                dtKv.Columns.Add(k.ToString());
            }
            dtKv.Rows.Add(new object[] { "Kv", Kv[1], Kv[2], Kv[3], Kv[4], Kv[5], Kv[6], Kv[7] });
            dtKv.Rows.Add(new object[] { "e", hsr[1], hsr[2], hsr[3], hsr[4], hsr[5], hsr[6], hsr[6] });
            dataGridView_kv_e.DataSource = dtKv;
            dataGridView_kv_e.DataBind();

        }

    }
}